/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include "umc_avs_enc_processing_unit_cpy.h"

#include "umc_automatic_mutex.h"
#include "umc_mutex.h"

namespace UMC
{

AVSCpyProcessingUnit::AVSCpyProcessingUnit(void)
{

} // AVSCpyProcessingUnit::AVSCpyProcessingUnit(void)

AVSCpyProcessingUnit::~AVSCpyProcessingUnit(void)
{

} // AVSCpyProcessingUnit::~AVSCpyProcessingUnit(void)

bool AVSCpyProcessingUnit::LoadJob(AVSListElement<AVSFrame> *pFrameList)
{
    // run over frame list and find the latest non-shown frame
    while (pFrameList)
    {
        if (false == pFrameList->IsShown())
        {
            if  (pFrameList->IsReady())
            {
                pFrameList->m_cpyCtx.m_pSrc = pFrameList;

                m_copier.SetCopyContext(pFrameList->m_cpyCtx);

                return true;
            }
            // wait until this frame is done
            else
            {
                break;
            }
        }

        // set the next frame
        pFrameList = pFrameList->GetNext();
    }

    return false;

} // bool AVSCpyProcessingUnit::LoadJob(AVSListElement<AVSFrame> *pFrameList)

Status AVSCpyProcessingUnit::DoJob(void)
{
    // do stream copying
    m_copier.CopyFrame();

    // finalize processed task
    UnloadJob();

    return UMC_OK;

} // Status AVSCpyProcessingUnit::DoJob(void)

Status AVSCpyProcessingUnit::HandleError(void)
{
    // finalize processed task
    UnloadJob();

    return UMC_OK;

} // Status AVSCpyProcessingUnit::HandleError(void)

void AVSCpyProcessingUnit::UnloadJob(void)
{
    AutomaticMutex guard(m_pGuard->ExtractHandle());

    m_copier.GetCopyContext().m_pSrc->SetShown();

} // void AVSCpyProcessingUnit::UnloadJob(void)

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_ENCODER)
